//
//  UIViewController+GQHPermission.m
//  Xanthium
//
//  Created by GuanQinghao on 04/05/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#import "UIViewController+GQHPermission.h"
#import "UIViewController+GQHAlert.h"
#import "GQHConfigure.h"

#import "GQHDriverProfileController.h"

#import "GQHAuthorizationController.h"
#import "GQHDriverAuthorizationController.h"
#import "GQHOwnerAuthorizationController.h"
#import "GQHEnterpriseAuthorizationController.h"



@implementation UIViewController (GQHPermission)

/// 检测用户类型
- (GQHUserType)qh_checkUserType {
    
    if ([USER_GET_TYPE isEqualToString:USER_TYPE_D]) {
        
        /// 司机
        return GQHUserTypeDriver;
    }
    
    if ([USER_GET_TYPE isEqualToString:USER_TYPE_O]) {
        
        /// 货主
        return GQHUserTypeOwner;
    }
    
    if ([USER_GET_TYPE isEqualToString:USER_TYPE_S]) {
        
        /// 无车承运人
        return GQHUserTypeShipper;
    }
    
    if ([USER_GET_TYPE isEqualToString:USER_TYPE_E]) {
        
        /// 物流企业
        return GQHUserTypeEnterprise;
    }
    
    /// 未知
    return GQHUserTypeDefault;
}

/// 检测用户权限
- (GQHAccessPermissionType)qh_checkAccessPermissionType {
    
    if ([USER_GET_STATUS isEqualToString:USER_STATUS_S0]) {
        
        // 全部功能
        return GQHAccessPermissionTypeAdvanced;
    }
    
    if ([USER_GET_STATUS isEqualToString:USER_STATUS_S4]) {
        
        // 部分功能
        return GQHAccessPermissionTypeJunior;
    }
    
    // 不能访问
    return GQHAccessPermissionTypeNone;
}

/// 检测用户访问权限
- (void)qh_checkAccessPermissionType:(GQHAccessPermissionType)accessPermissionType allowed:(void(^)(void))allowedBlock unallowed:(void(^)(void))unallowedBlock {
    
    if ([self qh_checkAccessPermissionType] >= accessPermissionType) {
        
        allowedBlock();
    } else {
        
        if ([self qh_checkAccessPermissionType] == GQHAccessPermissionTypeJunior) {
            
            [self qh_alertWithTitle:nil message:@"当前用户信息尚未认证成功\n请认证成功后继续操作" actionTitle:@"认证" handler:^(UIAlertAction *action) {
                
                [self qh_submitDriverAuthorizedInformation];
            } completion:nil];
        }
        
        if ([self qh_checkAccessPermissionType] == GQHAccessPermissionTypeNone) {
            
            if ([self qh_checkUserType] == GQHUserTypeDriver) {
                
                if ([USER_GET_STATUS isEqualToString:USER_STATUS_S2]) {
                    
                    [self qh_alertWithTitle:nil message:@"当前用户尚未提交基本信息\n请提交基本信息后继续操作" actionTitle:@"提交信息" handler:^(UIAlertAction *action) {
                        
                        [self qh_submitDriverAuthorizedInformation];
                    } completion:nil];
                } else {
                    
                    [self qh_alertWithTitle:nil message:@"当前用户信息尚未认证成功\n请认证成功后继续操作" actionTitle:@"查看" handler:^(UIAlertAction *action) {
                        
                        [self qh_checkAuthorizedInformation];
                    } completion:nil];
                }
            } else {
                
                if ([USER_GET_STATUS isEqualToString:USER_STATUS_S2]) {
                    
                    [self qh_alertWithTitle:nil message:@"当前用户信息尚未认证成功\n请认证成功后继续操作" actionTitle:@"认证" handler:^(UIAlertAction *action) {
                        
                        if ([self qh_checkUserType] == GQHUserTypeOwner) {
                            
                            [self qh_submitOwnerAuthorizedInformation];
                        } else if ([self qh_checkUserType] == GQHUserTypeEnterprise) {
                            
                            [self qh_submitEnterpriseAuthorizedInformation];
                        } else {
                            
                            return;
                        }
                    } completion:nil];
                } else {
                    
                    [self qh_alertWithTitle:nil message:@"当前用户信息尚未认证成功\n请认证成功后继续操作" actionTitle:@"查看" handler:^(UIAlertAction *action) {
                        
                        [self qh_checkAuthorizedInformation];
                    } completion:nil];
                }
            }
        }
    }
}

/// 司机提交基本信息
- (void)qh_submitDriverProfile {
    
    GQHDriverProfileController *drvierProfileController = [[GQHDriverProfileController alloc] init];
    drvierProfileController.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:drvierProfileController animated:YES];
}

/// 司机提交认证信息
- (void)qh_submitDriverAuthorizedInformation {
    
    GQHDriverAuthorizationController *driverAuthorizationController = [[GQHDriverAuthorizationController alloc] init];
    driverAuthorizationController.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:driverAuthorizationController animated:YES];
}

/// 货主提交认证信息
- (void)qh_submitOwnerAuthorizedInformation {
    
    GQHOwnerAuthorizationController *ownerAuthorizationController = [[GQHOwnerAuthorizationController alloc] init];
    ownerAuthorizationController.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:ownerAuthorizationController animated:YES];
}

/// 物流企业提交认证信息
- (void)qh_submitEnterpriseAuthorizedInformation {
    
    GQHEnterpriseAuthorizationController *enterpriseAuthorizationController = [[GQHEnterpriseAuthorizationController alloc] init];
    enterpriseAuthorizationController.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:enterpriseAuthorizationController animated:YES];
}

/// 查看认证信息
- (void)qh_checkAuthorizedInformation {
    
    GQHAuthorizationController *authorizationController = [[GQHAuthorizationController alloc] init];
    authorizationController.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:authorizationController animated:YES];
}

@end
